"""
Case Type   : show collation
Case Name   : 查看不同用户是否有权限执行改语法
Create At   : 2024/10/25
Owner       : lonely-dance
Description :
    1.使用普通用户执行改语法
    2.使用系统管理员用户执行改语法
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShowCollation(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_b_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()

    def test_show_collation(self):
        text = '-----step1:使用普通用户执行改语法 expect:查询成功-----'
        self.log.info(text)
        cmd = """
            drop user if exists test_user_013a;
            create user test_user_013a password 'test@1234';
            alter session set session authorization test_user_013a password 'test@1234';
            show collation;
        """
        self.log.info(cmd)
        res = self.pri_b_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertTrue(self.constant.CREATE_ROLE_SUCCESS_MSG in res and 'ERROR' not in res, '执行失败' + text)

        text = '-----step2:使用系统管理员用户执行改语法 expect:查询成功-----'
        self.log.info(text)
        cmd = """
            ALTER SESSION SET SESSION AUTHORIZATION default;
            drop user if exists test_user_013b;
            create user test_user_013b sysadmin password 'test@1234';
            alter session set session authorization test_user_013b password 'test@1234';
            show collation;
        """
        self.log.info(cmd)
        res = self.pri_b_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertTrue(self.constant.CREATE_ROLE_SUCCESS_MSG in res and 'ERROR' not in res, '执行失败' + text)

    def tearDown(self):
        text = '-----setp4:清理环境 expect:成功-----'
        self.log.info(text)
        cmd = """
            ALTER SESSION SET SESSION AUTHORIZATION default;
            drop user test_user_013a;
            drop user test_user_013b;
        """
        self.log.info(cmd)
        res = self.pri_b_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertTrue(res.count(self.constant.DROP_ROLE_SUCCESS_MSG) == 2, '执行失败' + text)

        self.log.info(f'----{os.path.basename(__file__)} end----')
